package com.example.char04;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.view.ViewDebug;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/6/22.
 */

public class AppInfoParser {
    public static List<AppInfo>getAppInfos(Context context){
        PackageManager pm=context.getPackageManager();
        List<PackageInfo> packageInfos=pm.getInstalledPackages(0);
        List<AppInfo> appInfos=new ArrayList<AppInfo>();
        for (PackageInfo packageInfo:packageInfos){
            AppInfo appInfo=new AppInfo();
            String  packname=packageInfo.packageName;
            appInfo.PackageName=packname;
            Drawable icon=packageInfo.applicationInfo.loadIcon(pm);
            appInfo.icon=icon;
            String appname=packageInfo.applicationInfo.sourceDir;
            appInfo.appName=appname;
            //应用程序apk包的路径
            String apkpath=packageInfo.applicationInfo.sourceDir;
            appInfo.apkPath=apkpath;
            File file=new File(apkpath);
            long appSize=file.length();
            appInfo.appSize=appSize;
            //应用程序安装位置
            int flags =packageInfo.applicationInfo.flags;//二进制映射
            if((ApplicationInfo.FLAG_EXTERNAL_STORAGE & flags)!=0){
                //外部存储
                appInfo.isInRoom=false;
            }else {
                //手机内存
                appInfo.isInRoom=true;

            }
            if((ApplicationInfo.FLAG_SYSTEM & flags)!=0){
                //系统应用
                appInfo.isUserApp=false;
            }else {
                //用户应用
                appInfo.isUserApp=true;
            }
            appInfos.add(appInfo);
            appInfo=null;

        }
        return  appInfos;
    }
}
